home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / inn1.000 / inn1.4sec-linux-src.tar / inn / lib / localopen.c < prev    next >
C/C++ Source or Header  |  1993-01-29  |  2KB  |  88 lines

  1. /*  $Revision: 1.8 $
  2. **
  3. */
  4. #include <stdio.h>
  5. #include <errno.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include "configdata.h"
  9. #include "paths.h"
  10. #include "clibrary.h"
  11. #include "nntp.h"
  12. #include "macros.h"
  13. #if    defined(DO_HAVE_UNIX_DOMAIN)
  14. #include <sys/un.h>
  15. #endif    /* defined(DO_HAVE_UNIX_DOMAIN) */
  16.  
  17.  
  18. /*
  19. **  Open a connection to the local InterNetNews NNTP server and optionally
  20. **  create stdio FILE's for talking to it.  Return -1 on error.
  21. */
  22. int
  23. NNTPlocalopen(FromServerp, ToServerp, errbuff)
  24.     FILE        **FromServerp;
  25.     FILE        **ToServerp;
  26.     char        *errbuff;
  27. {
  28. #if    defined(DO_HAVE_UNIX_DOMAIN)
  29.     int            i;
  30.     int            j;
  31.     int            oerrno;
  32.     struct sockaddr_un    server;
  33.     FILE        *F;
  34.     char        mybuff[NNTP_STRLEN + 2];
  35.     char        *buff;
  36.  
  37.     buff = errbuff ? errbuff : mybuff;
  38.     *buff = '\0';
  39.  
  40.     /* Create a socket. */
  41.     if ((i = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
  42.     return -1;
  43.  
  44.     /* Connect to the server. */
  45.     (void)memset((POINTER)&server, 0, sizeof server);
  46.     server.sun_family = AF_UNIX;
  47.     (void)strcpy(server.sun_path, _PATH_NNTPCONNECT);
  48.     if (connect(i, (struct sockaddr *)&server, AF_UNIX_SOCKSIZE(server)) < 0) {
  49.     oerrno = errno;
  50.     (void)close(i);
  51.     errno = oerrno;
  52.     return -1;
  53.     }
  54.  
  55.     /* Connected -- now make sure we can post. */
  56.     if ((F = fdopen(i, "r")) == NULL) {
  57.     oerrno = errno;
  58.     (void)close(i);
  59.     errno = oerrno;
  60.     return -1;
  61.     }
  62.     if (fgets(buff, sizeof mybuff, F) == NULL) {
  63.     oerrno = errno;
  64.     (void)fclose(F);
  65.     errno = oerrno;
  66.     return -1;
  67.     }
  68.     j = atoi(buff);
  69.     if (j != NNTP_POSTOK_VAL && j != NNTP_NOPOSTOK_VAL) {
  70.     (void)fclose(F);
  71.     /* This seems like a reasonable error code to use... */
  72.     errno = EPERM;
  73.     return -1;
  74.     }
  75.  
  76.     *FromServerp = F;
  77.     if ((*ToServerp = fdopen(dup(i), "w")) == NULL) {
  78.     oerrno = errno;
  79.     (void)fclose(F);
  80.     errno = oerrno;
  81.     return -1;
  82.     }
  83.     return 0;
  84. #else
  85.     return NNTPconnect(LOOPBACK_HOST, FromServerp, ToServerp, errbuff);
  86. #endif    /* defined(DO_HAVE_UNIX_DOMAIN) */
  87. }
  88.